package com.xlink.service.impl;

import com.xlink.domain.XlinkUser;
import com.xlink.repository.XlinkUserRepository;
import com.xlink.service.XlinkUserService;
import org.moon.base.service.AbstractDomainService;
import org.moon.rbac.domain.User;
import org.moon.utils.Objects;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;


/**
 * Created by Cathy on 2015/12/14.
 */
@Service
public class XlinkUserServiceImpl extends AbstractDomainService<XlinkUser> implements XlinkUserService {

    @Resource
    private XlinkUserRepository xlinkUserRepository;

    @Override
    public User register(String userName, String password, String email){
        XlinkUser user = new XlinkUser();
        user.setUserName(userName);
        user.setPassword(password);
        user.setEmail(email);
        user.encryptPassword();
        xlinkUserRepository.register(user);
        return user;
    }
/*
    @Override
    public User  register(String userName, String password, String email, Date registerTime, int status){
        XlinkUser user = new XlinkUser();
        user.setUserName(userName);
        user.setPassword(password);
        user.setEmail(email);
        user.setRegisterTime(new Date());
        user.setStatus(0);
        user.encryptPassword();
        user.setValidateCode(MD5.getMD5AsString(email));
        xlinkUserRepository.register(user);
        StringBuffer sb=new StringBuffer("点击下面链接激活账号，48小时生效，否则重新注册账号，链接只能使用一次，请尽快激活！</br>");
        sb.append("<a href=\"http://localhost:8080/springmvc/user/register?action=activate&email=");
        sb.append(email);
        sb.append("&validateCode=");
        sb.append(user.getValidateCode());
        sb.append("\">http://localhost:8080/springmvc/user/register?action=activate&email=");
        sb.append(email);
        sb.append("&validateCode=");
        sb.append(user.getValidateCode());
        sb.append("</a>");
        SendEmail.send(email, sb.toString());
        return user;
    }
*/
    @Override
    public boolean isUserNameExists(String userName){
        return xlinkUserRepository.isUserNameExists(userName)>0;
    }

    @Override
    public boolean isemailRegistered(String email){
        return xlinkUserRepository.isemailRegistered(email)>0;
    }

    @Override
    public XlinkUser login(String userName,String password) {
        XlinkUser user = new XlinkUser();
        user.setUserName(userName);
        user.setPassword(password);
        user.encryptPassword();
        Long userId = xlinkUserRepository.login(user);
        if(Objects.nonNull(userId)){
            user.setId(userId);
            return user;
        }else{
            return null;
        }
    }

    @Override
    public void updateUser(Map<String,Object> params){
         xlinkUserRepository.updateUser(params);
    }

    @Override
    public Map<String,Object> getById(Long id){
        return xlinkUserRepository.getById(id);
    }


/*
    @Override
    public void processActivate(String email,String validateCode)throws ServiceException{
        XlinkUser user = getByEmail(email);
        if(user!=null){
            if(user.getStatus()==0){
                Date currentTime = new Date();
                //currentTime.before(user.getRegisterTime());
                //if(currentTime.before(user.getLastActivateTime())){
                if(validateCode.equals(user.getValidateCode())){
                    System.out.println("==sq==="+user.getStatus());
                    user.setStatus(1);
                    System.out.println("==sh==="+user.getStatus());
                    xlinkUserRepository.update(user);
                }else{
                    throw new ServiceException("激活码不正确");
                }
                //}else{
                //    throw new ServiceException("激活码已过期！");
                //}
            }else{
                throw new ServiceException("邮箱已激活，请登录！");
            }
        }else{
            throw new ServiceException("该邮箱未注册（邮箱地址不存在）！");
        }
    }

    private XlinkUser getByEmail(String email){
        // Status RegisterTime ValidateCode
        Map<String,Object> userMap = xlinkUserRepository.getByEmail(email);
        XlinkUser user = new XlinkUser();
        user.setValidateCode(Objects.toString(userMap.get("validateCode")));
        user.setStatus(Objects.toInt(userMap.get("status"),0));

        return user;
    }*/
}

